
 
package com.junitUtil.spring;	
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.AnnotationTransactionAttributeSource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor;
import org.springframework.transaction.interceptor.TransactionInterceptor;
import com.junitUtil.baseTest.AbstractBaseTest;

import java.util.Properties;
import java.util.concurrent.TimeUnit;

import javax.sql.DataSource;

import org.mockito.Mockito;
import org.mybatis.spring.SqlSessionFactoryBean;



/**
 * TODO(描述这个类的作用) 
 * 
 **/
@Configuration
@EnableTransactionManagement(proxyTargetClass = true)
@TransactionConfiguration
@EnableAspectJAutoProxy
public class TestDataBaseConfig {
    @Bean
    public PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
    	PropertyPlaceholderConfigurer config = new PropertyPlaceholderConfigurer();
        config.setLocation((new DefaultResourceLoader()).getResource(AbstractBaseTest.dataBaseBean.configPropertiesPath));
        return config;
    }

    DataSource dataSource1 = null;
    DataSourceTransactionManager dataSourceTransactionManager1=null;
    BeanFactoryTransactionAttributeSourceAdvisor advisor=null;
    AnnotationTransactionAttributeSource annotationTransactionAttributeSource=null;
    TransactionInterceptor transactionInterceptor=null;
    public DataSource getDataSource1() {
        if (dataSource1 == null) {
            dataSource();
        }
        return dataSource1;
    }
    public DataSourceTransactionManager getDataSourceTransactionManager(){
        if(dataSourceTransactionManager1==null){
            dataSourceTransactionManager1=new DataSourceTransactionManager(getDataSource1());
        }
        return dataSourceTransactionManager1;
    }
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        DataBaseBean bean = AbstractBaseTest.dataBaseBean ;
        dataSource.setDriverClassName(bean.jdbcDriver);
        dataSource.setUrl(bean.jdbcUrl);
        dataSource.setUsername(bean.jdbcUsername);
        dataSource.setPassword(bean.jdbcPassword);
        Properties properties=new Properties();
        properties.setProperty("","");
        dataSource.setConnectionProperties(properties);
        dataSource1 = dataSource;
        return dataSource;
    }

    @Bean(name = "sqlSessionFactory")
    public SqlSessionFactoryBean sqlSessionFactoryBean() throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean() ;
        sessionFactory.setDataSource(getDataSource1());
        sessionFactory.afterPropertiesSet();
        return sessionFactory ;
    }

    @Bean(name = "transactionManager")
    public DataSourceTransactionManager aataSourceTransactionManager() {
        return getDataSourceTransactionManager();
    }
//  //手动注册spring事务管理相关类
//  @Bean(name = TransactionManagementConfigUtils.TRANSACTION_ADVISOR_BEAN_NAME)
//  @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
//  public BeanFactoryTransactionAttributeSourceAdvisor transactionAdvisor() {
//    if(advisor!=null){
//        return advisor;
//    }
//      advisor = new BeanFactoryTransactionAttributeSourceAdvisor();
//      advisor.setTransactionAttributeSource(  annotationTransactionAttributeSource());
//      advisor.setAdvice(transactionInterceptor());
//      return advisor;
//  }
//  //手动注册spring事务管理相关类
//  @Bean 
//  @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
//  public AnnotationTransactionAttributeSource annotationTransactionAttributeSource() {
//    if(annotationTransactionAttributeSource!=null){
//          return annotationTransactionAttributeSource;
//      }
//    annotationTransactionAttributeSource = new AnnotationTransactionAttributeSource();
//      return annotationTransactionAttributeSource;
//  }
//  //手动注册spring事务管理相关类
//  @Bean
//  @Role(BeanDefinition.ROLE_INFRASTRUCTURE)
//  public TransactionInterceptor transactionInterceptor() {
//    if(transactionInterceptor!=null){
//          return transactionInterceptor;
//      }
//    transactionInterceptor = new TransactionInterceptor();
//    transactionInterceptor.setTransactionAttributeSource(annotationTransactionAttributeSource());
//    transactionInterceptor.setTransactionManager(getDataSourceTransactionManager());
//      return transactionInterceptor;
//  }
    @Bean
    public RedisTemplate redisTemplate() {
    	RedisTemplate redisTemplate=Mockito.mock(RedisTemplate.class);
		ValueOperations valueOperations=Mockito.mock(ValueOperations.class);
		Mockito.doReturn(valueOperations).when(redisTemplate).opsForValue();
		Mockito.doReturn(null).when(valueOperations).get(Mockito.anyString());
		Mockito.doNothing().when(valueOperations).set( Mockito.anyString(),Mockito.anyObject(),Mockito.anyLong(),Mockito.any(TimeUnit.class));
		return redisTemplate;
    }
    
    
  
}

